package com.ITXu.reggie.controller;

import java.util.List;

import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ITXu.reggie.DTO.DishDto;
import com.ITXu.reggie.DTO.SetmealDto;
import com.ITXu.reggie.commom.R;
import com.ITXu.reggie.entity.Category;
import com.ITXu.reggie.entity.Dish;
import com.ITXu.reggie.entity.Setmeal;
import com.ITXu.reggie.entity.SetmealDish;
import com.ITXu.reggie.service.CategoryService;
import com.ITXu.reggie.service.SetmealDishService;
import com.ITXu.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/setmeal")
public class SetmealController {

	@Autowired
	private SetmealService setmealService;
	
	@Autowired
	private SetmealDishService setmealDishService;
	
	@Autowired
	private CategoryService categoryService;
	
	/**
	 * 新增套餐
	 * @param setmealDto
	 * @return
	 */
	@PostMapping
	public R<String> save(@RequestBody SetmealDto setmealDto) {
		setmealService.saveWithDish(setmealDto);
		return R.success("新增套餐成功");
	}
	
	/**
	 * 根据单一条件进行分页信息查询
	 * @param page
	 * @param pageSize
	 * @param name
	 * @return
	 */
	@GetMapping("/page")
	public R<Page> page(int page, int pageSize, String name) {
		//构造分页构造器
		Page<Setmeal> pageInfo = new Page<>(page, pageSize);
		//构造泛型为DishDto的分页构造器
		Page<SetmealDto> DtoPageInfo = new Page<>(page, pageSize);
		//构造条件查询器
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		//注入查询条件
		queryWrapper.like(name != null, Setmeal::getName, name);
		queryWrapper.orderByDesc(Setmeal::getUpdateTime);
		//调用page方法,完善pageInfo属性
		setmealService.page(pageInfo, queryWrapper);
		//把pageInfo中除records之外的属性赋值到DtoPageInfo(records是一个dish集合)
		BeanUtils.copyProperties(pageInfo, DtoPageInfo, "records");
		//把records中的setmeal对象通过map转换成有categoryName属性的setmealdto对象,此时返回的records重新赋值到一个泛型为DishDto的集合 list 中
		List<Setmeal> records = pageInfo.getRecords();
		List<SetmealDto> list = null;
		list = records.stream().map((item)-> {
			SetmealDto setmealDto = new SetmealDto();
			BeanUtils.copyProperties(item, setmealDto);
			//通过categoryId获得对应的category对象，取得categoryName
			Long categoryId = item.getCategoryId();
			Category category = categoryService.getById(categoryId);
			if (category != null) {
				String categoryName = category.getName();
				setmealDto.setCategoryName(categoryName);
			}
			return setmealDto;
	    }).collect(Collectors.toList());
		//把list赋值给DtoPageInfo
		DtoPageInfo.setRecords(list);
		return R.success(DtoPageInfo);
	}
	
	/**
	 * 根据id查找对应的setmeal和setmealdish集合
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R<SetmealDto> getSetmealDtoById(@PathVariable Long id) {
		//查找setmeal
		Setmeal setmeal = setmealService.getById(id);
		//查找setmealdish集合
		LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(id != null, SetmealDish::getSetmealId , id);
		List<SetmealDish> setmealDishes =  setmealDishService.list(queryWrapper);
		//给setmealDto赋值
		SetmealDto dto = new SetmealDto();
		BeanUtils.copyProperties(setmeal, dto);
		dto.setSetmealDishes(setmealDishes);
		return R.success(dto);
	}
	
	/**
	 * 保存修改的setmeal和setmealdish
	 * @param setmealDto
	 * @return
	 */
	@PutMapping
	public R<String> update(@RequestBody SetmealDto setmealDto) {
		setmealService.updateWithDish(setmealDto);
		return R.success("修改套餐成功");
	}
	
	/**
	 * 根据ids集合删除setmeal和setmealdish
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	public R<String> delete(@RequestParam List<Long> ids) {
		setmealService.removeWithDish(ids);
		return R.success("成功删除当前套餐");
	}
	
	/**
	 * 根据ids更改status属性
	 * @param ids
	 * @return
	 */
	/*@PostMapping("/status")
	//public R<String> changeStatus(@RequestParam Integer doStatus, @RequestParam List<Long> ids) {
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(Setmeal::getId, ids);
		Integer status = null;
		if (doStatus != null) {
			status = 0;
		}
		status = 1;
		queryWrapper.eq(Setmeal::getStatus, status);
		return R.success("状态修改成功");
	}*/
	
	@GetMapping("/list")
	public R<List<Setmeal>> list(Setmeal setmeal) {
		LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
		queryWrapper.eq(setmeal.getStatus() != 0, Setmeal::getStatus, setmeal.getStatus());
		queryWrapper.orderByDesc(Setmeal::getUpdateTime);
		List<Setmeal> setmeals = setmealService.list(queryWrapper);
		return R.success(setmeals);
	}
 }
